package com.itany.netclass.service.proxy;

import com.itany.netclass.entity.User;
import com.itany.netclass.exception.*;
import com.itany.netclass.factory.ObjectFactory;
import com.itany.netclass.service.UserService;
import com.itany.netclass.tran.TransactionManager;

import java.util.List;

/**
 * Author:sjy@itany.com
 * Date:21-10-12 下午5:13
 * Version:1.0-SNAPSHOT
 * Description:
 */
public class UserServiceProxy implements UserService {

    @Override
    public User login(String loginName, String password, String yzm, String code) throws ServiceException, NullSringException, CodeException, NameOrPasswordException, NoQuanXianException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        UserService userService = (UserService) ObjectFactory.getObject("UserServiceTarget");
        User user=null;
        try {
            tran.begin();
            user = userService.login(loginName, password, yzm, code);
            tran.commit();
        } catch (NullSringException e) {
            tran.rollback();
            throw e;
        } catch (CodeException e) {
            tran.rollback();
            throw e;
        } catch (NameOrPasswordException e) {
            tran.rollback();
            throw e;
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        } catch (NoQuanXianException e) {
            tran.rollback();
            throw e;
        }
        return user;
    }

    @Override
    public List<User> findAllUser(User user) throws ServiceException, NoLoginException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        UserService userService = (UserService) ObjectFactory.getObject("UserServiceTarget");
        List<User> list= null;
        try {
            tran.begin();
            list=userService.findAllUser(user);
            tran.commit();
            return list;
        } catch (ServiceException e) {
            tran.rollback();
           throw e;
        } catch (NoLoginException e) {
            tran.rollback();
            throw e;
        }
    }

    @Override
    public User findUserById(String id) throws ServiceException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        UserService userService = (UserService) ObjectFactory.getObject("UserServiceTarget");
        User user=null;
        try {
            tran.begin();
            user=userService.findUserById(id);
            tran.commit();
            return user;
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        }

    }

    @Override
    public void modifyUserBy(String id, String username, String roleName, String password, String email) throws ServiceException, NullSringException, TpyeException, EmailExistException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        UserService userService = (UserService) ObjectFactory.getObject("UserServiceTarget");

        try {
            tran.begin();
            userService.modifyUserBy(id,username,roleName,password,email);
            tran.commit();
        } catch (NullSringException e) {
            tran.rollback();
            throw e;
        } catch (TpyeException e) {
            tran.rollback();
            throw e;
        } catch (EmailExistException e) {
            tran.rollback();
            throw e;
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        }
    }

    @Override
    public void modifyStatusById(String id, String status) throws ServiceException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        UserService userService = (UserService) ObjectFactory.getObject("UserServiceTarget");
        try {
            tran.begin();
            userService.modifyStatusById(id,status);
            tran.commit();
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        }
    }

    @Override
    public List<User> sreachUserByForm(String username, String userNickname, String email, String roleName, String kaishitime, String jieshutime, String kaishitime1, String jieshutime1) throws ServiceException, NullSringException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        UserService userService = (UserService) ObjectFactory.getObject("UserServiceTarget");
        try {
            tran.begin();
            List<User> users = userService.sreachUserByForm(username, userNickname, email, roleName, kaishitime, jieshutime, kaishitime1, jieshutime1);
            tran.commit();
            return users;
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        } catch (NullSringException e) {
            tran.rollback();
            throw e;
        }

    }

    @Override
    public void modifyUserBy2(String id, String username, String roleName, String password, String email) throws NullSringException, TpyeException, EmailExistException, ServiceException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        UserService userService = (UserService) ObjectFactory.getObject("UserServiceTarget");

        try {
            tran.begin();
            userService.modifyUserBy2(id,username,roleName,password,email);
            tran.commit();
        } catch (NullSringException e) {
            tran.rollback();
            throw e;
        } catch (TpyeException e) {
            tran.rollback();
            throw e;
        } catch (EmailExistException e) {
            tran.rollback();
            throw e;
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        }
    }

    @Override
    public void jianchaUsername(String nickname) throws ServiceException, NullSringException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        UserService userService = (UserService) ObjectFactory.getObject("UserServiceTarget");
        try {
            tran.begin();
            userService.jianchaUsername(nickname);
            tran.commit();
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        } catch (NullSringException e) {
            tran.rollback();
            throw e;
        }
    }

    @Override
    public void jianchaRoleName(String roleName) throws ServiceException, TpyeException, NullSringException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        UserService userService = (UserService) ObjectFactory.getObject("UserServiceTarget");
        try {
            tran.begin();
            userService.jianchaRoleName(roleName);
            tran.commit();
        } catch (TpyeException e) {
            tran.rollback();
            throw e;
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        } catch (NullSringException e) {
            tran.rollback();
            throw e;
        }
    }

    @Override
    public void jianchaEmail(String email,String id) throws TpyeException, NullSringException, ServiceException, EmailExistException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        UserService userService = (UserService) ObjectFactory.getObject("UserServiceTarget");
        try {
            tran.begin();
            userService.jianchaEmail(email,id);
            tran.commit();
        } catch (TpyeException e) {
            tran.rollback();
            throw e;
        } catch (NullSringException e) {
            tran.rollback();
            throw e;
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        } catch (EmailExistException e) {
            tran.rollback();
            throw e;
        }

    }
}
